//
//  CDMoreViewController.m
//  CuriosityDaily
//
//  Created by JSY on 16/9/23.
//  Copyright © iconX16年 theBestTeam. All rights reserved.
//
#define MoreButtonHW 44
#define leftMargin 24
#define bottomMargin 28
#define buttonTime 0.3
#define viewTime 0.2
#define topViewH 176
#define bottomH 491
#define iconHW 44
#define iconX 30
#define iconY  600

#import "CDMoreViewController.h"
#import "CDtopView.h"
#import "CDBottomView.h"
#import "CDSearcherController.h"
#import "CDRightCategoryView.h"

@interface CDMoreViewController ()<CDtopViewDelegate>
//记录上面的view
@property(nonatomic,weak)UIView *topView;
//记录下面的view
@property(nonatomic,weak)UIView *bottomView;
//记录searchBar
@property(nonatomic,weak)UIButton *searchBar;

@property(nonatomic,weak)UIView* BlackView;
@property(nonatomic,assign)BOOL isNight;
@property(nonatomic,weak)UITableView *tableView;

@end

@implementation CDMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self popAnimationWithToCenterY:CDScreen.size.height - bottomMargin - MoreButtonHW*.5];
}

#pragma mark //设置主要的视图
-(void)setupUI{

//    self.preferredStatusBarStyle = 
    self.view.backgroundColor = [UIColor blackColor];
    self.view.alpha = 0.8;
      [self setTopView];
      [self setupBottomView];
      [self setCancelBtn];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cagegoryShow) name:@"cagegory" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(rightBtnClick) name:@"rightBtnClick" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cancelClick) name:@"cancelClick" object:nil];
    
}
#pragma mark 通知实现方法

-(void)cagegoryShow{
    [UIView animateWithDuration:viewTime animations:^{
        self.rightCategoryView.frame = CGRectMake(0, topViewH,self.view.bounds.size.width, self.bottomView.bounds.size.height);
    }];
    [UIView animateWithDuration:viewTime animations:^{
        self.bottomView.frame = CGRectMake(- self.view.bounds.size.width, topViewH, self.view.bounds.size.width, bottomH);
    }];
    [UIView animateWithDuration:buttonTime animations:^{
        self.CancelBtn.frame = CGRectMake(-self.view.bounds.size.width, iconY, iconHW, iconHW);
    }];
    [self setBackBtn];
}
-(void)rightBtnClick{
    [self CancelBtn];
}
-(void)cancelClick{
    [self CancelBtnClick];

}
#pragma mark 创建返回按钮
-(void)setBackBtn{
    UIButton *BackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [BackBtn addTarget:self action:@selector(BackBtnClick) forControlEvents:UIControlEventTouchUpInside];
    BackBtn.frame = CGRectMake(self.view.bounds.size.width + iconX, iconY, iconHW, iconHW);
    [BackBtn setImage:[UIImage imageNamed:@"homeBackButton@2x.png"] forState:UIControlStateNormal];
    self.backBtn = BackBtn;
    [self.view addSubview:BackBtn];
    [UIView animateWithDuration:buttonTime animations:^{
        self.backBtn.frame = CGRectMake(iconX, iconY, iconHW, iconHW);
    }];
}
-(void)BackBtnClick{
    
    [UIView animateWithDuration:viewTime animations:^{
        self.rightCategoryView.frame = CGRectMake(self.view.bounds.size.width, topViewH,self.view.bounds.size.width, self.bottomView.bounds.size.height);
    }];
    [UIView animateWithDuration:viewTime animations:^{
        self.bottomView.frame = CGRectMake(0, topViewH,self.view.bounds.size.width, self.bottomView.bounds.size.height);
    }];
    [UIView animateWithDuration:buttonTime animations:^{
        self.CancelBtn.frame = CGRectMake(iconX, iconY, iconHW, iconHW);
    }];
    [UIView animateWithDuration:buttonTime animations:^{
    self.backBtn.frame = CGRectMake(self.view.bounds.size.width + iconX, iconY, iconHW, iconHW);
    }];
    
}

#pragma mark //添加底部视图
-(void)setupBottomView{
    CDBottomView *bottomView = [[CDBottomView alloc]init];
    bottomView.backgroundColor = [UIColor clearColor];
    bottomView.frame = CGRectMake(0, 667, self.view.bounds.size.width, bottomH);
      [self.view addSubview:bottomView];
    self.bottomView = bottomView;

    self.tableView = bottomView.tableView;
    
    
    
    //创建新闻分类view
    CDRightCategoryView *cdRightCategorView = [[CDRightCategoryView alloc]init];
    cdRightCategorView.frame = CGRectMake(self.view.bounds.size.width, 167, self.view.bounds.size.width, 167);
    
    [self.view addSubview:cdRightCategorView];
    self.rightCategoryView = cdRightCategorView;

 
}
#pragma mark 设置顶部视图
-(void)setTopView{
    
    CDtopView *topView = [[CDtopView alloc]init];
    
    topView.delegate = self;
    topView.backgroundColor = [UIColor clearColor];
    topView.frame = CGRectMake(0, -topViewH, self.view.bounds.size.width, topViewH);
    [self.view addSubview:topView];
    //button的宽和高
    
    UIButton *searchBar = [UIButton buttonWithType:UIButtonTypeCustom];
   
    [topView addSubview:searchBar];
    searchBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, 64);
    [searchBar setImage:[UIImage imageNamed:@"SearchBar@2x.png"] forState:UIControlStateNormal];
    [searchBar addTarget:self action:@selector(searchBarClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.topView = topView;
    self.searchBar = topView.searchBar;
}

#pragma mark 弹出两个view
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    [UIView animateWithDuration:viewTime animations:^{
        self.bottomView.frame = CGRectMake(0, topViewH,self.view.bounds.size.width, self.bottomView.bounds.size.height);
    }];
    [UIView animateWithDuration:viewTime animations:^{
        self.topView.frame = CGRectMake(0, 0,self.view.bounds.size.width, topViewH);
        self.searchBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, 64);
    }];
}
#pragma mark //创建取消按钮
-(void)setCancelBtn{
    UIButton *CancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CancelBtn.alpha = 1;
    [CancelBtn addTarget:self action:@selector(CancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    CancelBtn.frame = CGRectMake(leftMargin, CDScreen.size.height, MoreButtonHW, MoreButtonHW);
    [CancelBtn setBackgroundImage:[UIImage imageNamed:@"closeButton.png"] forState:UIControlStateNormal];
    self.CancelBtn = CancelBtn;
    [self.view addSubview:CancelBtn];
}

#pragma mark 返回按钮的点击
-(void)CancelBtnClick{
    [self popAnimationWithToCenterY:(CDScreen.size.height + MoreButtonHW * 0.5)];
    [UIView animateWithDuration:viewTime animations:^{
        self.bottomView.frame = CGRectMake(0, 667, self.view.bounds.size.width, bottomH);
        self.topView.frame = CGRectMake(0, -topViewH, self.view.bounds.size.width, topViewH);
        self.searchBar.frame = CGRectMake(0, -64, self.view.bounds.size.width, 64);
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
        if (self.dismissBlock != nil) {
            self.dismissBlock();
        }
    }];
}
#pragma mark //轻点手势实现
-(void)tapClick{
    
    [self CancelBtnClick];
}
#pragma mark pop动画
-(void)popAnimationWithToCenterY:(CGFloat)centerY{
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(leftMargin + MoreButtonHW*.5, centerY)];
    
    anim.springBounciness = 12;
    anim.springSpeed = 10;
    //    anim?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
    anim.beginTime = CACurrentMediaTime();
    
    [self.CancelBtn pop_addAnimation:anim forKey:nil];
}
#pragma mark 搜索按钮的点击
-(void)searchBarClick{
    CDSearcherController *searchVC = [[CDSearcherController alloc] init];
    
    [self presentViewController:searchVC animated:YES completion:nil];
//    [self.view removeFromSuperview];
}
#pragma mark 代理的实现
-(void)CDtopView:(CDtopView *)cdtopView didClickHomeTopViewButtonWithType:(ZFBHomeTopViewButtonType)type andImageName:(UIImageView *)imageView andLabel:(UILabel *)label{
    switch (type) {
        case ZFBHomeTopViewButtonTypeScan:
            NSLog(@"设置");
            break;
        case ZFBHomeTopViewButtonTypePay:
            NSLog(@"夜间");
            if ([label.text isEqualToString:@"夜间" ] && !self.isNight) {
                imageView.image = [UIImage imageNamed:@"sidebar_switchNight_night@2x.png"];
                label.text = @"白天";
                [self setNightModel];
            }else{
                imageView.image = [UIImage imageNamed:@"sidebar_switchNight@2x.png"];
                label.text = @"夜间";
                [self setDay];
            }
            break;
        case ZFBHomeTopViewButtonTypeCard:
            NSLog(@"离线");
            break;
        case ZFBHomeTopViewButtonTypeXiu:
            NSLog(@"推荐");
            break;
            
        default:
            break;
    }

}
#pragma mark 设置夜间模式
-(void)setNightModel{
    UIView *view = [[UIView alloc]init];
    view.userInteractionEnabled = NO;
    view.backgroundColor = [UIColor blackColor];
    view.frame = self.view.frame;
    view.alpha = 0.8;
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    self.BlackView = view;
}
#pragma mark 设置白天模式
-(void)setDay{
    self.BlackView.backgroundColor = [UIColor clearColor];
    self.BlackView.alpha = 0.01;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    if (self.BlackView) {
        self.isNight = YES;
    }
    
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
@end
